package steak.xteamy.mixin;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import steak.xteamy.KillerGroup;
import steak.xteamy.VoiceChat.FunctionComponent;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {

    @Unique ItemStack self = (ItemStack) (Object) this;

    @Inject(method = "use", at = @At("HEAD"))
    private void onUse(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
        MinecraftServer server = user.getServer();
        if (server != null) {
            FunctionComponent functionComponent = self.get(KillerGroup.USE_FUNCTION_COMPONENT);
            if (functionComponent != null) {
                functionComponent.apply(server, user, hand);
            }
        }
    }
}
